package com.docted.model.helloworld;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.hibernate.service.ServiceRegistry;

import javax.persistence.EntityManagerFactory;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;

/**
 * @author DoCted
 * @date 2017/8/23
 */
public class HelloWorldHibernate {
//    SessionFactory sessionFactory = new MetadataSources(
//            new StandardServiceRegistryBuilder()
//                    .configure("hibernate.cfg.xml").build())
//            .buildMetadata().buildSessionFactory();





    public void init() {
        //生成器
        StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
        //配置服务注册表
        standardServiceRegistryBuilder
                .applySetting("hibernate.connection.datasource", "hibernate")
                .applySetting("hibernate.format_sql", "true")
                .applySetting("hibernate.use_sql_comments", "true")
                .applySetting("hibernate.hbm2ddl.auto", "create-drop");
        ServiceRegistry serviceRegistry = standardServiceRegistryBuilder.build();


        MetadataSources metadataSources = new MetadataSources(serviceRegistry);
        metadataSources.addAnnotatedClass(com.docted.model.helloworld.Message.class);
        MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();

        Metadata metadata = metadataBuilder.build();

        SessionFactory sessionFactory = metadata.buildSessionFactory();

        Session session = sessionFactory.getCurrentSession();
        Message message = new Message();
        message.setText("Hi");

        session.persist(message);
    }

}
